import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:sleep/db/merroy_db.dart';
import 'package:sleep/provider/wave_deatil_provider/sleep_detail_provider.dart';

enum Sleeptate {
  normal,
  mild,
  moderate,
  severe,
}

extension MyState on Sleeptate {
  static Sleeptate get(int count) {
    if (count > 30) return Sleeptate.severe;
    if (count > 15) return Sleeptate.moderate;
    if (count > 5) return Sleeptate.mild;
    return Sleeptate.normal;
  }
}

int before = DateTime.now().millisecondsSinceEpoch;

final sleepOsashProvider = Provider<Sleeptate?>((ref) {
  final sleep = ref.watch(sleepDetailProvider);
  final current = DateTime.now().millisecondsSinceEpoch;

  if (current - before > 2000 && sleep.wave != null) {
    if (sleep.wave!.isRealSt()) MerroyDB.share().stCount += 1;
    if (sleep.wave!.isLowRi()) MerroyDB.share().lowCount += 1;

    return MyState.get(MerroyDB.share().stCount - MerroyDB.share().lowCount);
  }

  return null;
});
